package com.haochang.chunkvod.view.vod.menu;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.model.order.MenuInfo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

/**
 *@author qintao
 *created at 2016/6/4 15:52
 * 点餐菜单控件
 */

public class MenuLayout extends LinearLayout
{
	private Context context;
	private List<MenuView> menuViewList = new ArrayList<MenuView>();
	private int currentSelectedPos = 0;
	private OnTabChangedListener changedListener;

	public MenuLayout(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		this.context = context;
	}

	public MenuLayout(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		this.context = context;
	}

	public MenuLayout(Context context)
	{
		super(context);
		this.context = context;
	}

	public void addTab(MenuInfo info)
	{
		int total = this.getChildCount();
		String menuId = info.getMenuId();
		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		MenuView menuView = new MenuView(context);
		menuView.setLayoutParams(params);
		menuView.setTag(total);
		menuView.setTag(R.id.menu_value_id, menuId);
		menuView.setTag(R.id.menu_is_present, info.getIs_present());
		menuView.setText(info.getMenuName());
		this.addView(menuView);
		menuView.setOnClickListener(clickListener);
		this.menuViewList.add(menuView);
		if (total == 0)
			menuView.setSelected(true);
		else
			menuView.setSelected(false);
	}

	public void setOnTabChangedListener(OnTabChangedListener changedListener)
	{
		this.changedListener = changedListener;
	}

	private OnBaseClickListener clickListener = new OnBaseClickListener()
	{

		@Override
		public void onBaseClick(View v)
		{
			int pos = (Integer) v.getTag();
			String menuId = (String) v.getTag(R.id.menu_value_id);
			int is_present = (Integer) v.getTag(R.id.menu_is_present);
			setSelectedTable(pos, menuId, is_present);
		}
	};

	public boolean setSelectedTable(int position, String menuId, int is_present)
	{
		if (currentSelectedPos != position && null != this.menuViewList && this.menuViewList.size() > position)
		{
			int oldPos = currentSelectedPos;
			currentSelectedPos = position;

			for (int i = 0; i < this.menuViewList.size(); i++)
			{
				if (i == position)
					this.menuViewList.get(i).setSelected(true);
				else
					this.menuViewList.get(i).setSelected(false);
			}

			if (changedListener != null)
			{
				changedListener.onTabChanged(oldPos, currentSelectedPos, menuId, is_present);
			}
			return true;
		}
		return false;
	}

	/**
	 * 获取当前的tab position
	 * @return
	 */
	public int getCurrentTab()
	{
		return currentSelectedPos;
	}

	public interface OnTabChangedListener
	{
		void onTabChanged(int last, int current, String menuId, int is_present);
	}

	/**
	 * 设置提醒灯是否显示
	 * @param postionTab table位置
	 * @param visible 可见性 {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}
	 */
	public void setRemindImageAtPos(int postionTab, int visible, int orderNum)
	{
		if (null != this.menuViewList && this.menuViewList.size() > postionTab)
			this.menuViewList.get(postionTab).setRemindVisibility(visible, orderNum);
	}

}
